package com.fkdx.service;

import com.fkdx.common.enums.SysStatusEnum;
import com.fkdx.dao.condition.DictCondition;
import com.fkdx.dao.entity.Dict;
import com.fkdx.dao.entity.PageResult;
import com.fkdx.dao.mapper.DictMapper;
import com.fkdx.service.dto.DictQueryReq;
import com.fkdx.service.dto.DictQueryResp;
import com.fkdx.service.dto.DictReq;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import com.google.common.collect.Lists;
import com.fkdx.common.dto.sys.CurrentUserDTO;
import com.fkdx.common.sys.SecurityHelper;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.Date;

/**
 * @author: create by wsh 
 * @date:2022/8/20
 */
@Log4j2
@Service()
public class DictService {

    @Resource
    private DictMapper dictMapper;

    /**
     * 获取粮食品种集合     *     * @param req     * @return
     */
    public PageResult<DictQueryResp> getDictList(DictQueryReq req) {
        CurrentUserDTO principal = SecurityHelper.getPrincipal();
        DictCondition condition = new DictCondition();
        BeanUtils.copyProperties(req, condition);
        condition.setTenantId(principal.getTenantId());
        List<Dict> list = dictMapper.selectAll(condition);
        PageResult<DictQueryResp> results = new PageResult<>(list, (m) -> {
              DictQueryResp res = new DictQueryResp();
              BeanUtils.copyProperties(m, res);
              return res;
            });
        return results;
    }

    /**
     * 获取详情     *     * @param id     * @return
     */
    public DictQueryResp getDictForm(Long id) {
        DictQueryResp resp = new DictQueryResp();
        Dict dict = dictMapper.selectByPrimaryKey(id);
        BeanUtils.copyProperties(dict, resp);
        return resp;
    }

    /**
     * 新增、修改     *     * @param req
     */
    public void saveDictForm(DictReq req) {
        Dict dict = new Dict();
        BeanUtils.copyProperties(req, dict);
        CurrentUserDTO principal = SecurityHelper.getPrincipal();
        Date d = new Date();
        if (req.getId() == null) {
            dict.setCreated(d);
            dict.setModified(d);
            dict.setTenantId(principal.getTenantId());
            dict.setYn(SysStatusEnum.YES.getStatus());
            dictMapper.insert(dict);
        } else {
            dictMapper.updateByPrimaryKey(dict);
        }
    }

    /**
     * 删除     *     * @param id
     */
    public void removeDictList(Long[] id) {
        dictMapper.updateByIds(Lists.newArrayList(id));
    }

    public String getCode(String code) {
        Dict dict = dictMapper.selectByCode(code);

        return dict.getDictName().concat(dict.getDataName().replace("${val}", dict.getDataCode()));
    }

    public DictQueryResp getDictCode(String code) {
        DictQueryResp resp = new DictQueryResp();
        Dict dict = dictMapper.selectByCode(code);
        BeanUtils.copyProperties(dict, resp);
        return resp;
    }

    public Map<String, DictQueryResp> getDictMap(List<String> codes) {
        List<Dict> list = dictMapper.selectDictMap(codes);
        Map<String, DictQueryResp> ret = new HashMap<>(list.size());
        if (list != null && !list.isEmpty()) {
            for (Dict dict : list) {
                DictQueryResp resp = new DictQueryResp();
                BeanUtils.copyProperties(dict, resp);
                ret.put(resp.getDictCode(), resp);
            }
        }
        return ret;
    }
}
